<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <base data-ice="baseUrl" href="../../../">
  <title data-ice="title">lib/model/index.js | API Document</title>
  <link type="text/css" rel="stylesheet" href="css/style.css">
  <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
  <script src="script/prettify/prettify.js"></script>
  
  
  <script src="script/manual.js"></script>
</head>
<body class="layout-container" data-ice="rootContainer">

<header>
  <a href="./">Home</a>
  
  <a href="identifiers.html">Reference</a>
  <a href="source.html">Source</a>
  
  <a data-ice="repoURL" href="https://git.oschina.net/kinuxroot/unique-model.js.git">Repository</a>
  <div class="search-box">
  <span>
    <img src="./image/search.png">
    <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
  </span>
    <ul class="search-result"></ul>
  </div>
</header>

<nav class="navigation" data-ice="nav"><div>
  <ul>
    
  <li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/session.js~Session.html">Session</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-createSession">createSession</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-enablePersist">enablePersist</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-isPersistEnabled">isPersistEnabled</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-BaseModel">BaseModel</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Types">Types</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-model">model</a></span></span></li>
<li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">backend</div><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/backend/base.js~Dao.html">Dao</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/backend/base.js~Session.html">Session</a></span></span></li>
<li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">model</div><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/model/base.js~Model.html">Model</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-createModel">createModel</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getModel">getModel</a></span></span></li>
<li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">type</div><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/type/index.js~BaseType.html">BaseType</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/type/index.js~Double.html">Double</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/type/index.js~Integer.html">Integer</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/type/index.js~Text.html">Text</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/lib/type/index.js~UObject.html">UObject</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-defineType">defineType</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-toUniqueModel">toUniqueModel</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-allTypes">allTypes</a></span></span></li>
</ul>
</div>
</nav>

<div class="content" data-ice="content"><h1 data-ice="title">lib/model/index.js</h1>
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">&apos;use strict&apos;

/**
 * @private
 */
let allModels = Object.create(null);

/**
 * Get the registered model by model name.
 *
 * @public
 *
 * @param {String} modelName - The name of model
 *
 * @return {class&lt;model/base/Model&gt;}    The model whose name is matched with argument.
 *                      Return undefined if the model name is not registered.
 *
 * @example
 * let User = um.model.getModel(&apos;User&apos;);
 */
function getModel(modelName) {
    return allModels[modelName];
}

/**
 * Create the UniqueModel class from schema
 *
 * @public
 *
 * @param {String} modelName - The name of model.
 * @param {String} schema - The schema of model.
 *
 * @return {class&lt;mode/base/Model&gt;}    The model created according to the schema.
 *
 * @example
 * let Image = um.model.createModel(&apos;Image&apos;, {
 *     path: Types.Text(),
 *     type: Types.Text(),
 *     width: Types.Integer(),
 *     height: Types.Integer()
 * });
 *
 * let User = um.model.createModel(&apos;User&apos;, {
 *     name: Types.Text(),
 *     age: Types.Integer(),
 *     height: Types.Double(),
 *     weight: Types.Double(),
 *     photo: Types.UObject({
 *         type: &apos;Image&apos;
 *     })
 * });
 */
function createModel(modelName, schema) {
    let um = require(&apos;..&apos;);
    let Base = require(&apos;./base&apos;);
    let BaseModel = Base.Model;
    let ModelStatus = BaseModel.Status;
    let Types = um.Types;

    // &#x6A21;&#x578B;&#x7C7B;
    /**
     * The model class generated by createModel.
     * It will extend um/Base/Model.
     *
     * static _meta: &#x6A21;&#x578B;&#x4FE1;&#x606F;
     *     name: &#x6A21;&#x578B;&#x540D;
     *     schema: &#x6A21;&#x578B;&#x6A21;&#x5F0F;
     *     dao: &#x6570;&#x636E;&#x8BBF;&#x95EE;&#x5BF9;&#x8C61;
     * 
     * @public
     */
    class Model extends BaseModel {
        constructor() {
            super();

            /**
             * The class of this model.
             *
             * @public
             * @type {class&lt;um/base/Model} 
             */
            Object.defineProperty(this, 
                &apos;_class&apos;, {
                    value: Model,
                    writable: false,
                    enumerable: false,
                    configurable: false,
            });

            /**
             * The object to store data of unique object.
             *
             * @public
             * @type {class&lt;um/base/Model} 
             */
            Object.defineProperty(this, 
                &apos;_data&apos;, {
                    value: Object.create(null),
                    writable: true,
                    enumerable: false,
                    configurable: false,
            });

            /**
             * The id of object.
             *
             * @public
             * @type {String} 
             */
            Object.defineProperty(this, 
                &apos;id&apos;, {
                    get() {
                        return this._id;
                    },
                    set(id) {
                        /**
                         * The id of object. the id property will access this private property.
                         *
                         * @private
                         * @type {String} 
                         */
                        this._id = String(id);
                    },
                    enumerable: true,
                    configurable: false,
            });

            /**
             * The status of object.
             *
             * @public
             * @type {um/base/Model.Status} 
             */
            this._status = ModelStatus.New;

            /**
             * Specify whether the model has loaded completely.
             *
             * @public
             * @type {Boolean} 
             */
            this._complete = false;
        }

        /**
         * Convert the unique model object to string.
         *
         * @public
         * @return {String} The String representation of unique model object.
         */
        toString() {
            let value = Object.create(null);
            let schema = Model._meta.schema;
            Object.keys(schema).forEach(fieldName =&gt; {
                value[fieldName] = this[fieldName];
            });
            value.id = this.id;

            return value;
        }
    }

    Object.defineProperty(Model, 
        &apos;_meta&apos;, {
            value: {
                name: modelName,
                schema: schema
            },
            writable: false,
            enumerable: false,
            configurable: false,
    });

    // Create the properties of object according to the fields of schema.
    Object.keys(schema).forEach(fieldName =&gt; {
        let fieldType = schema[fieldName];
        /**
         * Operate the property of object.
         *
         * @public
         * @type {*}
         */
        Object.defineProperty(Model.prototype, 
            fieldName, {
                get() {
                    // If the model is created by a persistent session
                    // and it has not loaded completely,
                    // then we refresh the object to load all data.
                    // Or we return the value of field directly.
                    if ( this._dao &amp;&amp; this._complete === false ) {
                        this._dao.refresh(this);
                    }
                    return this._data[fieldName];
                },
                set(value) {
                    this._data[fieldName] = fieldType.fromValue(value);
                },
                enumerable: true,
                configurable: true
            }
        );
    });

    // Register the model using modelName.
    allModels[modelName] = Model;

    return Model;
}

module.exports = {
    createModel,
    getModel
};
</code></pre>

</div>

<footer class="footer">
  Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.4.3)</span></a>
</footer>

<script src="script/search_index.js"></script>
<script src="script/search.js"></script>
<script src="script/pretty-print.js"></script>
<script src="script/inherited-summary.js"></script>
<script src="script/test-summary.js"></script>
<script src="script/inner-link.js"></script>
<script src="script/patch-for-local.js"></script>
</body>
</html>
